home *** CD-ROM | disk | FTP | other *** search
- /* virusCheck.c -- Functions for self-diagnosis of virus infections.
- Copyright © 1989 by Michael S. Morton.
- You may copy, alter, use, and distribute these routines if you
- leave this file unchanged up to this line.
-
- See notes in the “.h” file.
-
- Think C 3.0 version.
- */
-
- /* History:
- 26-Nov-89 -- MM -- No longer needs strings library.
- Various small documentation changes.
- 6-Nov-89 -- MM -- First version.
-
- Enhancements needed:
- • Should we do a ReleaseResource for some resources, to ditch the
- master pointer which gets allocated only because we asked for it?
- How can we know when to do this? For CODE resources, it’s not a
- big problem, since there aren’t vast numbers of them.
- • Consider checking if the ROM/System file is recent enough for us
- */
-
- #include "virusCheck.h" /* get our own prototypes */
-
- /* Local prototypes: */
- static void fail (char *kind, ResType type, long actual, long expected);
- static void append (char **pPtr, char *s);
-
- /* vResCheck -- Check that the resources of a specified type in the application
- haven’t been altered. Return TRUE if there’s apparent tampering.
- */
- extern Boolean vResCheck (type, expectedCount, expectedLen, report)
- register ResType type; /* INPUT: type of resource to sum */
- register short expectedCount; /* INPUT: expected number of resources */
- register long expectedLen; /* INPUT: exp. total len of resources */
- register short report; /* INPUT: >0 => report errors w/debugger */
- { register short actCount; /* actual count of rsrcs of this type */
- register long actLen; /* actual total length of resources */
- register Handle rsrc; /* resource to check */
-
- register Boolean failFlag = false; /* any problems encountered? */
-
- register short oldResFile; /* for preserving current resource file */
- register Boolean oldResLoad; /* for preserving “ResLoad” flag */
-
- /* Switch to the application’s resource file. Note that all resource
- calls from here on are the “one deep” calls from Inside Mac, vol. IV. */
- /* You can swap the order of the next two lines: */
- oldResFile = CurResFile (); /* remember initial resource file */
- oldResLoad = ResLoad; /* remember “ResLoad” state */
-
- /* You can swap the order of the next two lines: */
- UseResFile (CurApRefNum); /* search application for resources */
- SetResLoad (false); /* don’t load the resources right away */
-
- /* You can swap the order of the next two lines: */
- actLen = 0; /* initialize length */
- actCount = Count1Resources (type); /* how many of this type are there? */
-
- if (actCount != expectedCount)
- { if (report > 0) /* is the developer listening? */
- fail ("count", type, actCount, expectedCount);
- failFlag = true; /* TAMPERING DETECTED */
- } /* end of mismatched resource count */
-
- while (actCount) /* loop actCount down to 1 */
- { /* Get the resource’s handle, but don’t load it. */
- rsrc = Get1IndResource (type, actCount); /* see if it’s already in memory */
- if (! rsrc) /* not available? */
- { if (report > 0) /* is the developer listening? */
- DebugStr ("\pResource not available!");
- failFlag = true; /* error detected; ASSUME TAMPERING */
- goto EXIT; /* sorry, Dr. Dijkstra */
- }
-
- /* You can swap the order of the next two lines: */
- actLen += SizeResource (rsrc); /* sum up length of rsrcs of this type */
- --actCount; /* get next index number */
- } /* end of loop through resources */
-
- if (actLen != expectedLen)
- { if (report > 0) /* is the developer listening? */
- fail ("length", type, actLen, expectedLen);
- failFlag = true; /* TAMPERING DETECTED */
- }
-
- EXIT: /* goto here on tampering or error */
- /* You can swap the order of the next two lines: */
- UseResFile (oldResFile); /* restore original resource file */
- SetResLoad (oldResLoad); /* restore original loading state */
-
- return failFlag; /* TRUE => error or tampering */
- } /* end of vResCheck () */
-
- /* vCodeCheck -- Check that CODE resources haven’t been altered.
- */
- extern Boolean vCodeCheck (expectedCount, expectedLen, report)
- register short expectedCount; /* expected number of CODEs */
- register long expectedLen; /* expected total size of CODEs */
- register short report; /* INPUT: >0 => report errors w/debugger */
- {
- return vResCheck ('CODE', expectedCount, expectedLen, report);
- } /* end of vCodeCheck () */
-
- /* fail -- dump a string like:
- Got <kind> <actual> for resource type '<type>', instead of <expected>
- */
- static void fail (kind, type, actual, expected)
- char *kind; /* INPUT: "count" or "length" */
- ResType type; /* INPUT: resource type which failed */
- long actual, expected; /* INPUT: counts or lengths */
- { char buffer [100]; /* for accumulating output message */
- char *bufp; /* pointer into buffer[] */
- Str255 actualText, expectedText; /* formatted from params */
- union /* to get ResType to be like string */
- { char resName [5];
- ResType theType;
- } u;
-
- NumToString ((long) actual, & actualText);
- PtoCstr ((char *) & actualText);
- NumToString ((long) expected, & expectedText);
- PtoCstr ((char *) & expectedText);
- u.theType = type; /* set up resource type… */
- u.resName [4] = '\0'; /* …to be a NUL-ended C string */
-
- bufp = buffer; /* point to output buffer */
- append (& bufp, "Got ");
- append (& bufp, kind);
- append (& bufp, " ");
- append (& bufp, (char *) & actualText);
- append (& bufp, " for resource type '");
- append (& bufp, u.resName);
- append (& bufp, "' instead of ");
- append (& bufp, (char *) & expectedText);
- *bufp++ = '\0';
- CtoPstr (buffer);
- DebugStr (buffer);
- } /* end of fail () */
-
- /* append -- Append a string to an output buffer. This routine lets us
- avoid pulling in the “strings” library.
- */
- static void append (pPtr, s)
- char **pPtr; /* UPDATE: VAR ptr to output */
- register char *s; /* INPUT: string to append */
- { register char *p; /* output ptr */
- register char c;
-
- p = *pPtr; /* pick up output pointer */
- while (c = *s++) /* loop through all non-nulls… */
- *p++ = c; /* …storing them in buffer */
- *pPtr = p; /* return updated output pointer */
- } /* end of append () */
-